package nccloud.web.pu.poorder.action;

import nc.vo.ct.purdaily.entity.AggCtPuVO;
import nc.vo.pu.m21.entity.OrderVO;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pflow.PfUserObject;
import nc.vo.scmpub.res.billtype.POBillType;
import nc.vo.scmpub.util.AppInfoContext;
import nccloud.dto.scmpub.script.entity.SCMScriptResultDTO;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.ui.pattern.extbillcard.ExtBillCard;
import nccloud.framework.web.ui.pattern.extbillcard.ExtBillCardOperator;
import nccloud.pubitf.pu.service.IEmailService;
import nccloud.pubitf.riart.pflow.CloudPFlowContext;
import nccloud.pubitf.scmpub.commit.service.IBatchRunScriptService;

public class SendMailAction extends AbstractOrderSaveAction {

	@Override
	protected ExtBillCard doAfterCompare(ExtBillCard serverCard, ExtBillCard clientCard) {
		return serverCard;
	}

	private CloudPFlowContext getCloudPFlowContext(OrderVO[] vos, PfUserObject pfUserObject) {
		CloudPFlowContext context = new CloudPFlowContext();
		context.setActionName("SAVEBASE");
		context.setBillType(POBillType.Order.getCode());
		context.setBillVos((AggregatedValueObject[]) vos);
		context.setUserObj(new PfUserObject[] { pfUserObject });
		return context;
	}

	@Override
	protected SCMScriptResultDTO doScriptRemoteCall(OrderVO[] vos, PfUserObject pfUserObject,
			ExtBillCardOperator operator) throws BusinessException {
		try {
			CloudPFlowContext cloudContext = getCloudPFlowContext(vos, pfUserObject);
			AppInfoContext.setBtnCode("Save");
			OrderVO bill = ((OrderVO[]) vos)[0];
			bill = ((IEmailService) ServiceLocator.find(IEmailService.class)).SendMail(bill);

			SCMScriptResultDTO scriptResult = ((IBatchRunScriptService) ServiceLocator
					.find(IBatchRunScriptService.class)).runBacth(cloudContext, OrderVO.class);
			return scriptResult;
		} catch (Exception e) {
			throw new BusinessException(e);
		}
	}

}
